package io.serializable;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class Test2 {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        // 如果需要批量序列化多个对象,可以把对象放到一个可以序列化的集合中,将这个集合序列化
        // 集合和对象必须都要实现Serializable接口
        // method();
        // 反序列化
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("AdvancedCode/src/io/serializable/student.txt"));
        ArrayList<Student> list = (ArrayList<Student>) ois.readObject();
        System.out.println(list);
    }

    private static void method() throws IOException {
        ArrayList<Student> list = new ArrayList<>(List.of(
                new Student("卢本伟", 25),
                new Student("大司马", 23),
                new Student("周淑怡", 20)
        ));
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("AdvancedCode/src/io/serializable/student.txt"));
        oos.writeObject(list);
        oos.close();
    }
}
